// components/SocialDetail/SocialDetail.js
const app = getApp();
const usecloud = true; //是否使用 云开发 正式开发环境中使用 application.js 中的配置
const db = wx.cloud.database();
const timeutil = require('../../utils/TimeUtil.js');
const cwx = require('profunc.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    id: '',
    circleitem: null,
    content: '',
    comments: [],
    liked: true
  },

  lifetimes:{
    created(){
      var that = this;
    // console.log(options.id)
    //console.log(app.globalData.userInfo)
    wx.getStorage({
      key: 'selcircle',
      success(res) {
        //console.log(res)
        that.setData({
          circleitem: res.data
        }, () => {
          that.reqThumbs();
          that.reqComment();
        })
      }
    })
      
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    ViewImage(e) {
      wx.previewImage({
        urls: e.currentTarget.dataset.imglist,
        current: e.currentTarget.dataset.url
      });
    },
    pubcom(e) {
      var that = this;
      wx.showLoading({
        title: '上传中',
        mask: true
      })
      console.log(e)
      that.setData({
        content:e.detail
      },()=>{
        cwx.AddRemarks(that.data).then(res => {
          console.log(res);
          that.reqComment();
          //调用组件中的方法
          this.commentBottom = this.selectComponent("#commentBottom");
          this.commentBottom.handleCloseInput();
        })
      })
      
    },
    reqComment() {
      var that = this;
      if (usecloud) {
        db.collection('remarks_list').where({
          circle_id: that.data.circleitem._id
        }).get().then(res => {
          console.log(res)
          that.setData({
            comments: res.data,
            content: ''
          })
          wx.hideLoading();
        })
      } else {
        //非云开发请求写在这里
      }
    },
    ilike() {
      var that = this;
      if (usecloud) {
        wx.showLoading({})
        cwx.AddThumbs(that.data).then(ans => {
          var titem = that.data.circleitem;
          titem.thumbsnum = titem.thumbsnum + 1
          that.setData({
            liked: false,
            circleitem: titem
          })
          console.log(ans);
          wx.hideLoading()
        })
  
      } else {
        //非云开发处理方式写在这里
      }
    },
    textAreaBlur(e) {
      //console.log(e)
      this.setData({
        content: e.detail.value
      })
    },
    reqThumbs() {
      //请求看看自己是否喜欢
      var that = this;
      if (usecloud) {
        db.collection('thumbs_list').where({
          userid: app.globalData.openid,
          circle_id: that.data.circleitem._id
        }).get().then(res => {
          console.log(res)
          if (res.data.length != 0) {
            that.setData({
              liked: false
            })
          }
        })
      } else {
  
      }
    }
  }
})
